App.controller("activityLookCtrl",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	var params = JSON.parse($stateParams.params);
	$scope.search = {}; 
	$scope.details = {};     //人员列表的展示数据
	$scope.list= {};         //活动详情的展示  
	
	$scope.shopName_hidden = false;		//所属专卖店、报名专卖店（显示）
	
	var alertModel = {};	//alert模态框
	var brandIds = params.brandId;
	var stateData = {
			1:"物流",
			2:"安装",
			3:"售后",
			4:"关闭"
			
	}
	
	
	if(userInfo.user_level==0){	//开发者
		
	}else if(userInfo.user_level==3){	//三级用户
	
	}else if(userInfo.user_level==2){	//二级用户
		
	}else if(userInfo.user_level==1){	//一级用户
		$scope.shopName_hidden = true;	
	}
	
	
	
	//活动管理的展示具体内容
	$http({
		   url: path+"/server/selectActivityStatistics",  //brandIds
		   method: 'POST',
		   data:{
			   "activity_id":brandIds
		   },
			contentType : "application/json"
	}).success(function(data){
		var dataList = data.result;
		if(dataList.cardNum == null){
			$scope.list.card = 0;  //活动卡销售数量
		}else{
			$scope.list.card = dataList.cardNum;  //活动卡销售数量
		}
	
		$scope.list.sign_num = dataList.signNum;  //活动签到数量
		$scope.list.egg_ticket_use = dataList.eggShiYong;  //砸蛋券已使用
		$scope.list.egg_ticket_grant = dataList.eggNum;  //砸金蛋发放数量
		$scope.list.egg_ticket_num = dataList.eggUse;  //砸金蛋核销数量
		$scope.list.lottery_use = dataList.luckyShiYong;  //抽奖券已使用数量
		$scope.list.lottery_grant = dataList.luckyNum;  //抽奖券发放数量
		$scope.list.lottery_num = dataList.luckyUse;  //抽奖券核销数量
		$scope.list.prize_grant = dataList.giftNum;  //奖金发放数量
		$scope.list.amount_num = dataList.order_price;  //当前活动销售金额
	}).error(function(data){
		console.log(data);   
	});
	
	//活动管理
	$scope.gridOptions2 = {
			dataSource : {
				transport : {
					read : {
						url : path+"/server/selectActivityApplyListPage",
						type : 'POST',
						dataType : "json",
						contentType : "application/json"
					},
					parameterMap : function(options, operation) {
						if (operation == "read") {
							var sort = [{dir:"desc",field:"sign_time"}];
							if(options.sort!=null && options.sort.length>0 ){
								if(options.sort[0].field=="sign_time"){
									options.sort[0].field="sign_time";
								}else{
									options.sort[0].field="sign_time";
								}
								
								sort=options.sort;
							}
							var parameter = {
								page : options.page, //当前页
								pageSize : options.pageSize,
								param:{
									customerName:$scope.search.user_name,  //姓名
									phone :$scope.search.phone, //联系方式
									sign_state:$('#isSureSign').val(),//是否签到
									shopName:$scope.search.commercial_name,  //商户名称
									activity_id:brandIds,
									sort:sort
												
								}
							};
							console.log('查询参数',parameter);
							return kendo.stringify(parameter);
						}
					},
				},
				pageSize : 10,
				serverPaging : true,
				serverSorting: true,
				schema : {
					data : function(d) {
						return d.result.data;
					},
					total : function(d) {
						return d.result.total; //总条数
					}
				},
			},
			pageable : {  //分页
				pageSizes: [10, 20, 50, 100],  //每页显示记录数
			  },  
			sortable: {   //排序
				//allowUnsort: false,  //允许无序
			    mode: "multiple"     //排序模式：single，multiple
			  },
			editable: false,  //true：单个编辑；popup：弹出新页面编辑
			dataBound: function () {   //序号
		        var rows = this.items();
		        var page = this.pager.page() - 1;
		        var pagesize = this.pager.pageSize();
		        $(rows).each(function () {
		            var index = $(this).index() + 1 + page * pagesize;
		            var rowLabel = $(this).find(".row-number");
		            $(rowLabel).html(index);
		        });
		    },
			columns : [
					{    
						title : "序号",
						field : "customer_id", 
						width : "50px",
						template:"<input type='checkbox' class='recordClass' value='#: customer_id #' />"+"<span class='row-number subTitle'></span>"					
					},
					{
						field : "shopName",         
						title : "所属专卖店",     
						width : "100px",
						hidden: $scope.shopName_hidden
					},
					{
						field : "applyShop",         
						title : "报名专卖店",     
						width : "100px",
						hidden: $scope.shopName_hidden
					},
					{
						field : "customerName",         
						title : "客户名称",     
						width : "80px"
					},
					{
						field : "phone",         
						title : "联系方式",     
						width : "100px"
					},
					{
						field : "apply_time",         
						title : "报名日期",     
						width : "100px"
					},
					{
						field : "sign_state",         
						title : "签到状态",     
						width : "80px"
					},
					{
						field : "sign_time",         
						title : "签到日期",     
						width : "100px"
					},
					{
						field : "eggNum",         
						title : "金蛋券",     
						width : "60px"
					},
					{
						field : "eggShiYong",         
						title : "砸蛋券已使用",     
						width : "100px"
					},
					{
						field : "eggUse",         
						title : "核销数量/礼包",     
						width : "100px"
						
					},
					{
						field : "luckyNum",         
						title : "抽奖券",     
						width : "60px"
					},
					{
						field : "luckyShiYong",         
						title : "抽奖券已使用",     
						width : "100px"
					},
					{
						field : "luckyUse",         
						title : "核销数量/礼包",     
						width : "100px"
					},
					{
						field : "real_pay_price",         
						title : "实收金额",     
						width : "80px"
					},
					{
						field : "",         
						title : "操作",     
						width : "60px",
						template: function(e){
					    	var html = '';
				    		var	template_html ='<div class="operation k-state-default " ng-click="lookDetails(\''+e.customer_id+'\')">查看</div>';
							return template_html;
					    	if(userInfo.user_type==1){  //开发者
					    		var	template_html ='<div class="operation k-state-default " ng-click="lookDetails(\''+e.customer_id+'\')">查看</div>'
		            			return template_html;
					    	}else if(userInfo.user_type==2){  //三级商户 //二级用户
					    							+'<div class="operation k-state-default " ng-click="lookDetails(\''+e.customer_id+'\')">查看</div>'
								return template_html;
					    	}else if(userInfo.user_type==3){  //一级商户
					    		var	template_html ='<div class="operation k-state-default " ng-click="lookDetails(\''+e.customer_id+'\')">查看</div>'
										return template_html;					    		
					    	}else if(userInfo.user_type==4){  //一级商户
					    		
					    	}else if(userInfo.user_type==5){  //专卖店
					    		
					    	}

					    	return html;
					    },
					}

			]
	};
	//查询
	$scope.search = function(){
		$scope.grid2.dataSource.page(1);
	}
	//是否签到
	$scope.isSureSignIn = function(){
		$("#isSureSign").kendoDropDownList({
	        dataTextField: "text",
		    dataValueField: "id",
		    placeholder: "暂未有礼品选中",
		    dataSource: [
			             {
			            	 id: "",
			 				text: "请选择"
			             },
			             {
			            	 id: "1",
			 				text: "是"
			             },
			             {
			            	 id: "2",
			 				text: "否"
			             } 
		                 ],
	      	change: function(e) {
	      	},
		});
	};
	//全选，取消
	$scope.chooseAll = function(className,flag){
		publicService.chooseAllByClassName(className,flag);
	}
	
	//导出表格数据（1：筛选导出，2：选中导出）  
	$scope.downloadTableData = function(type){
		var paramObj = {};
		
		if(type==1){	//筛选导出
			paramObj = {
					page : 1, 
					pageSize : 10,
					ispage:false,
					param:{
						customerName:$scope.search.user_name,  //姓名
						phone :$scope.search.phone, //联系方式
						sign_state:$('#isSureSign').val(),//是否签到
						shopName:$scope.search.commercial_name,  //商户名称
						activity_id:brandIds
					}
			};
		}else if(type==2){	//选中导出
			var ids = publicService.getChooseValueByClassName("recordClass");
			if(ids.length==0){
				$scope.alertMessage("请选择参与用户！");
				return;
			}
			
			paramObj = {
					page : 1, 
					pageSize : 10,
					param:{
						ids: ids,
						id:brandIds
					}
			};
		}
		var requestUrl = path+"/server/export";		//请求路径
		var exportModule = "activityApplyList_module";			//模块名称
		var form = $("<form method='POST' action='"+requestUrl+"' target='_self'>"
						+"<input type='hidden' name='module_code' value='"+exportModule+"'/>"
						+"<input type='hidden' name='page' value='"+angular.toJson(paramObj)+"'/>"
					+"</form>");
		console.log("【表单参数】",form.serializeArray());
	    $(document.body).append(form);
	    form.submit();	
	};
	//提示信息
	$scope.alertMessage = function(message){
		alertModel.pannel = jqueryAlert({
			content: message,
			modal: true
		});
	};

	//活动参与人详情
	$scope.lookDetails = function(id){
		$scope.lookInfoWindow.center().open();   //打开弹框
		$http({
			   url: path+"/server/selectActivityUser",
			   method: 'POST',
			   data:{
				   activity_id:brandIds,
				   customer_id:id
			   },
				contentType : "application/json"
		}).success(function(data){
			var dataList = data.result;
			console.log(dataList);
			$scope.details.customer_name = dataList.customer_name;  //姓名
			$scope.details.phone = dataList.phone;  //联系方式
			$scope.details.apply_time = dataList.apply_time;  //报名日期
			if(dataList.card == 1){  //已购买
				$scope.details.name = "已购买";  //活动卡
			}else if(dataList.card == 2){  //未购买
				$scope.details.name ="未购买";  //活动卡
			}
			
			$scope.details.deposit_price = dataList.deposit_price;  //预约定金
			$scope.details.order_pricep = dataList.order_pricep;  //实付现金
			$scope.details.head_url = dataList.imgurl;  //头像
				
			if( dataList.sign_state == 0){ //已
				$scope.details.sign_state =='已签到';  //活动状态
			}else{
				$scope.details.sign_state = '未签到';  //活动状态
			}
			
			$scope.details.sign_time = dataList.sign_time;  //签到日期
			$scope.details.sign_gift = dataList.sign_gift;  //签到礼
			$scope.details.smashEge = dataList.jindan.cs;  //砸金蛋
			$scope.details.verification1 = dataList.jindan.hs;  //核销数量
			$scope.details.present = dataList.jindan.gift;  //礼品
			$scope.details.lottery_ticket = dataList.choujiang.cs;  //抽奖券
			$scope.details.verification2 = dataList.choujiang.hs;  //核销数量
			$scope.details.present2 = dataList.choujiang.gift;  //礼品
			if(dataList.lipin.gift){
				$scope.details.fullGif = dataList.lipin.gift;  //满赠礼
			}else{
				$scope.details.fullGif = "暂无";  //满赠礼
			}
			
			
			$scope.details.order_code = dataList.dingdan[0].order_code;  //订单编号
			$scope.details.create_time = dataList.dingdan[0].create_time;  //下单时间
			$scope.details.order_state = stateData[dataList.order_state];  //订单状态
			$scope.details.product_model = dataList.dingdan[0].product_model;  //产品型号
			$scope.details.order_price = dataList.dingdan[0].order_price;  //订单总额			
		}).error(function(data){
			console.log(data);   
		});
	}
	publicService.initDateTime("startTime");	//开始时间	
	publicService.initDateTime("endTime");	//开始时间	
	$scope.isSureSignIn();    //是否签到
});